; +
; NAME:
; gauge_flux_mozart_regrid
;
; PURPOSE:
; Reads and extracts data from a gauge flux file and then reformats it to mozart grid
;
; CATEGORY:
; regridding
;
; CALLING SEQUENCE:
; gauge_flux_mozart_regrid, NCNAME = ncName, INPUTFILES = InputFiles, SPECIES = Species
;
; INPUTS:
; NCNAME = Name of output file
; INPUTFILES = Files to read and regrid. These must all be of identical format.
; SPECIES = Name of the flux species. Defaults to a name based on the parent directory.
;
; OUTPUTS:
;
; SIDE EFFECTS:
;
; RESTRICTIONS:
; All input files must be of identical format
; Assumes that variables date and datasec exist
;
; PROCEDURE:
;
; MODIFICATION HISTORY:
;   Written by: Matt Rigby, University of Bristol, Nov 25, 2013
;   Heavily edited by Ann Stavert 2014 to make it generalised
;
; -

pro gauge_flux_mozart_regrid, NCNAME = ncName, INPUTFILES = InputFiles, SPECIES = Species, PS_FILE = PS_File

  ;Input file name
  If n_elements(InputFiles) eq 0 then $
    ;InputFiles = dialog_pickFile(path = '/data/GAUGE', MULTIPLE_FILES=1, filter = '*.nc*')
    InputFiles = dialog_pickFile(path = '/data/shared/GAUGE/', MULTIPLE_FILES=1, filter = '*.nc*')
    
  ; Determine the flux species if not given
  if n_elements(Species) eq 0 then begin
    Species = ''
    Read, Species, Prompt = 'Enter species name:'
    
  endif
  
  ;Create some info for the file
  Title = Species + '_emissions'
  Author='Ann Stavert'
  
  ; Extract information about the file
  ; This uses the first file and assumes that all the files are of the same format
  gauge_flux_extract_file_info, InputFiles[0], DimNames, DimSizes,  VarInfo, LON_NCNAME = lon_ncname,$
    LAT_NCNAME = lat_ncname, FLUX_NCNAME = flux_ncname,  DATE_NCNAME = date_ncname, DATESEC_NCNAME = datesec_ncname, $
    GLOBAL_ATTS= global_atts
  
  ; Check if the title was given in the global_atts
  if N_elements(global_atts) then $
    if total(strmatch(global_atts.names, 'title', /FOLD_CASE)) eq 1 then Title = (global_atts.atts)[where(strmatch(global_atts.names, 'title', /FOLD_CASE))]
  
    
    
  ;Get flux data
  for i = 0L, n_elements(InputFiles)-1 do begin
  
    flux_fname=InputFiles[i]
    
    ; set unzipped flag
    uzipped_flag = 0
    
    ; Check if it's zipped/gzipped and if so zip/gunzip it
    Endings = ['gz','zip']
    Key = where(strmatch(Endings, (strsplit(flux_fname,'.',/EXTRACT))[-1]))
    
    Case (Key) of
    
      ; gunzip
      0: begin
        OutFileName = strjoin((strsplit(flux_fname,'.',/EXTRACT))[0:-2],'.')
        FILE_GUNZIP, flux_fname, OutFileName, /verbose
        ;set unzipped flag
        unzipped_flag = 1
        
      end
      
      ; unzip
      1: begin
        OutFileName = strjoin((strsplit(flux_fname,'.',/EXTRACT))[0:-2],'.')
        FILE_UNZIP, flux_fname, OutFileName, /verbose
        ;set unzipped flag
        unzipped_flag = 1
        
      end
      
      ; Not zipped or unzipped
      else: begin
        OutFileName = flux_fname
        unzipped_flag= 0
      end
      
    endcase
    
    
    
    ; Extract the data from the flux file
    q=mr_ncget(flux_ncname, filename=OutFileName) ;kg/m2/s
    q_lat=mr_ncget(lat_ncname, filename=OutFileName)
    q_lon=mr_ncget(lon_ncname, filename=OutFileName)
    
    ; May need to fix the poles
    ; check if the poles 89.5 and -89.5
    if (q_lat[0] eq -89.75) and (q_lat[-1] eq 89.75) then begin
      q_lat[0] = -90
      q_lat[-1] = 90
    endif
    
    date=mr_ncget(date_ncname, filename=OutFileName)
    datesec=mr_ncget(datesec_ncname, filename=OutFileName)
  
  if strmatch(datesec_ncname, date_ncname) then datesec = intarr(N_elements(Date)) 
  
    ; Here need to split into 2D and 3D regridding
    if Size(q, /N_DIMENSIONS) eq 4 then begin
   
      ; 3D gridding!!!
      GAUGE_FLUX_MOZART_3D_REGRID, q_lon, q_lat, q, date, datesec, flux_ncname, outfilename, varinfo, species, $
       TITLE = Title, PS_FILE = PS_File
      
      
    endif else begin
    
    GAUGE_FLUX_MOZART_2D_REGRID, q_lon, q_lat, q, date, datesec, flux_ncname, outfilename, varinfo, species, $
      TITLE = Title
    
    endelse
    
    ; If the file had to be unzipped to extract the data then delete the unzipped file
    if unzipped_flag eq 1 then  FILE_DELETE, OutFileName
    
    
  endfor
  
  
  
end